package com.google.android.vending.licensing;

import android.content.Context;
import android.util.Log;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

public class ServerManagedPolicy
  implements Policy
{
  private static final String DEFAULT_MAX_RETRIES = "0";
  private static final String DEFAULT_RETRY_COUNT = "0";
  private static final String DEFAULT_RETRY_UNTIL = "0";
  private static final String DEFAULT_VALIDITY_TIMESTAMP = "0";
  private static final long MILLIS_PER_MINUTE = 60000L;
  private static final String PREFS_FILE = "com.android.vending.licensing.ServerManagedPolicy";
  private static final String PREF_LAST_RESPONSE = "lastResponse";
  private static final String PREF_MAX_RETRIES = "maxRetries";
  private static final String PREF_RETRY_COUNT = "retryCount";
  private static final String PREF_RETRY_UNTIL = "retryUntil";
  private static final String PREF_VALIDITY_TIMESTAMP = "validityTimestamp";
  private static final String TAG = "ServerManagedPolicy";
  private int mLastResponse;
  private long mLastResponseTime = 0L;
  private long mMaxRetries;
  private PreferenceObfuscator mPreferences;
  private long mRetryCount;
  private long mRetryUntil;
  private long mValidityTimestamp;

  public ServerManagedPolicy(Context paramContext, Obfuscator paramObfuscator)
  {
    this.mPreferences = new PreferenceObfuscator(paramContext.getSharedPreferences("com.android.vending.licensing.ServerManagedPolicy", 0), paramObfuscator);
    this.mLastResponse = Integer.parseInt(this.mPreferences.getString("lastResponse", Integer.toString(291)));
    this.mValidityTimestamp = Long.parseLong(this.mPreferences.getString("validityTimestamp", "0"));
    this.mRetryUntil = Long.parseLong(this.mPreferences.getString("retryUntil", "0"));
    this.mMaxRetries = Long.parseLong(this.mPreferences.getString("maxRetries", "0"));
    this.mRetryCount = Long.parseLong(this.mPreferences.getString("retryCount", "0"));
  }

  private Map<String, String> decodeExtras(String paramString)
  {
    HashMap localHashMap = new HashMap();
    try
    {
      Iterator localIterator = URLEncodedUtils.parse(new URI("?" + paramString), "UTF-8").iterator();
      while (localIterator.hasNext())
      {
        NameValuePair localNameValuePair = (NameValuePair)localIterator.next();
        localHashMap.put(localNameValuePair.getName(), localNameValuePair.getValue());
      }
    }
    catch (URISyntaxException localURISyntaxException)
    {
      Log.w("ServerManagedPolicy", "Invalid syntax error while decoding extras data from server.");
    }
    return localHashMap;
  }

  private void setLastResponse(int paramInt)
  {
    this.mLastResponseTime = System.currentTimeMillis();
    this.mLastResponse = paramInt;
    this.mPreferences.putString("lastResponse", Integer.toString(paramInt));
  }

  private void setMaxRetries(String paramString)
  {
    try
    {
      Long localLong2 = Long.valueOf(Long.parseLong(paramString));
      localLong1 = localLong2;
      this.mMaxRetries = localLong1.longValue();
      this.mPreferences.putString("maxRetries", paramString);
      return;
    }
    catch (NumberFormatException localNumberFormatException)
    {
      while (true)
      {
        Log.w("ServerManagedPolicy", "Licence retry count (GR) missing, grace period disabled");
        paramString = "0";
        Long localLong1 = Long.valueOf(0L);
      }
    }
  }

  private void setRetryCount(long paramLong)
  {
    this.mRetryCount = paramLong;
    this.mPreferences.putString("retryCount", Long.toString(paramLong));
  }

  private void setRetryUntil(String paramString)
  {
    try
    {
      Long localLong2 = Long.valueOf(Long.parseLong(paramString));
      localLong1 = localLong2;
      this.mRetryUntil = localLong1.longValue();
      this.mPreferences.putString("retryUntil", paramString);
      return;
    }
    catch (NumberFormatException localNumberFormatException)
    {
      while (true)
      {
        Log.w("ServerManagedPolicy", "License retry timestamp (GT) missing, grace period disabled");
        paramString = "0";
        Long localLong1 = Long.valueOf(0L);
      }
    }
  }

  private void setValidityTimestamp(String paramString)
  {
    try
    {
      Long localLong2 = Long.valueOf(Long.parseLong(paramString));
      localLong1 = localLong2;
      this.mValidityTimestamp = localLong1.longValue();
      this.mPreferences.putString("validityTimestamp", paramString);
      return;
    }
    catch (NumberFormatException localNumberFormatException)
    {
      while (true)
      {
        Log.w("ServerManagedPolicy", "License validity timestamp (VT) missing, caching for a minute");
        Long localLong1 = Long.valueOf(60000L + System.currentTimeMillis());
        paramString = Long.toString(localLong1.longValue());
      }
    }
  }

  public boolean allowAccess()
  {
    boolean bool = true;
    long l = System.currentTimeMillis();
    if (this.mLastResponse == 256)
      if (l <= this.mValidityTimestamp)
        bool = true;
    while ((this.mLastResponse != 291) || (l >= 60000L + this.mLastResponseTime) || ((l > this.mRetryUntil) && (this.mRetryCount > this.mMaxRetries)))
      return bool;
    return true;
  }

  public long getMaxRetries()
  {
    return this.mMaxRetries;
  }

  public long getRetryCount()
  {
    return this.mRetryCount;
  }

  public long getRetryUntil()
  {
    return this.mRetryUntil;
  }

  public long getValidityTimestamp()
  {
    return this.mValidityTimestamp;
  }

  public void processServerResponse(int paramInt, ResponseData paramResponseData)
  {
    if (paramInt != 291)
    {
      setRetryCount(0L);
      if (paramInt != 256)
        break label104;
      Map localMap = decodeExtras(paramResponseData.extra);
      this.mLastResponse = paramInt;
      setValidityTimestamp((String)localMap.get("VT"));
      setRetryUntil((String)localMap.get("GT"));
      setMaxRetries((String)localMap.get("GR"));
    }
    while (true)
    {
      setLastResponse(paramInt);
      this.mPreferences.commit();
      return;
      setRetryCount(1L + this.mRetryCount);
      break;
      label104: if (paramInt == 561)
      {
        setValidityTimestamp("0");
        setRetryUntil("0");
        setMaxRetries("0");
      }
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.google.android.vending.licensing.ServerManagedPolicy
 * JD-Core Version:    0.6.2
 */